package com.huawei.cloud.phone.management.common.validate;

import com.huawei.cloud.phone.management.common.BeanFactory;
import com.huawei.cloud.phone.management.common.exception.InvalidParamException;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class ValidatorUtils {
    public static void validate(Object data, Object... groups) {
        Errors errors = new BeanPropertyBindingResult(data, data.getClass().getSimpleName());
        Validator validator = BeanFactory.getBean(Validator.class);
        ValidationUtils.invokeValidator(validator, data, errors, groups);
        if (errors.hasErrors()) {
            throw new InvalidParamException(errors.getAllErrors().get(0).getDefaultMessage());
        }
    }
}
